package com.yundasys.lab16springbootplus.lab16springbootplus.importselect;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

/**
 * @PACKAGE_NAME: com.yundasys.lab16springbootplus.lab16springbootplus.importselect
 * @NAME: MySelectorAutoConfiguration
 * @USER: jiangta
 * @DATE: 2022/9/22
 * @TIME: 9:57 上午
 * @PROJECT_NAME: springboot-labs
 */
public class MySelectorConfiguration implements BeanDefinitionRegistryPostProcessor, EnvironmentAware {

    private Environment env;

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(IndexDao.class);
        beanDefinitionRegistry.registerBeanDefinition("indexDao",builder.getRawBeanDefinition());
        System.out.println(env.getProperty("mySelector.config.enable"));
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        IndexDao indexDao = configurableListableBeanFactory.getBean("indexDao", IndexDao.class);
        BeanDefinition indexDao1 = configurableListableBeanFactory.getBeanDefinition("indexDao");
        indexDao.setName("name test");
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }
}
